package com.ruoyi.seedlingSys.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.seedlingSys.mapper.PatchMapper;
import com.ruoyi.seedlingSys.domain.Patch;
import com.ruoyi.seedlingSys.domain.Qr;
import com.ruoyi.seedlingSys.service.IPatchService;
import com.ruoyi.seedlingSys.mapper.ZhongmiaoMapper;
import com.ruoyi.seedlingSys.mapper.QrMapper;
import com.ruoyi.seedlingSys.domain.Zhongmiao;
import org.springframework.transaction.annotation.Transactional;

/**
 * 批次管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class PatchServiceImpl implements IPatchService {
    @Autowired
    private PatchMapper patchMapper;

    @Autowired
    private ZhongmiaoMapper zhongmiaoMapper;

    @Autowired
    private QrMapper qrMapper;

    /**
     * 查询批次管理
     * 
     * @param patchID 批次管理主键
     * @return 批次管理
     */
    @Override
    public Patch selectPatchByPatchID(Long patchID) {
        return patchMapper.selectPatchByPatchID(patchID);
    }

    /**
     * 查询批次管理列表
     * 
     * @param patch 批次管理
     * @return 批次管理
     */
    @Override
    public List<Patch> selectPatchList(Patch patch) {
        return patchMapper.selectPatchList(patch);
    }

    /**
     * 新增批次管理
     * 
     * @param patch 批次管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPatch(Patch patch) {
        // 验证关联的种苗记录是否存在
        if (patch.getPatchZhongMiaoID() != null) {
            Zhongmiao zhongmiao = zhongmiaoMapper.selectZhongmiaoByZhongMiaoID(patch.getPatchZhongMiaoID());
            if (zhongmiao == null) {
                throw new RuntimeException("关联的种苗记录不存在");
            }
        }

        // 验证其他关联记录...

        patch.setCreateTime(DateUtils.getNowDate());
        return patchMapper.insertPatch(patch);
    }

    /**
     * 修改批次管理
     * 
     * @param patch 批次管理
     * @return 结果
     */
    @Override
    public int updatePatch(Patch patch) {
        patch.setUpdateTime(DateUtils.getNowDate());
        return patchMapper.updatePatch(patch);
    }

    /**
     * 批量删除批次管理
     * 
     * @param patchIDs 需要删除的批次管理主键
     * @return 结果
     */
    @Override
    public int deletePatchByPatchIDs(Long[] patchIDs) {
        return patchMapper.deletePatchByPatchIDs(patchIDs);
    }

    /**
     * 删除批次管理信息
     * 
     * @param patchID 批次管理主键
     * @return 结果
     */
    @Override
    public int deletePatchByPatchID(Long patchID) {
        return patchMapper.deletePatchByPatchID(patchID);
    }

    /**
     * 通过二维码uuid获取二维码信息，并从中拿到批次ID，然后根据批次ID获取批次信息
     * 
     * @param uuid
     * @return
     */
    public Patch getPatchByQrCode(String qrUniqueCode) {
        // 1. 通过uuid获取二维码信息
        List<Qr> qrList = qrMapper.selectQrByQrUniqueCode(qrUniqueCode);
        if (qrList == null || qrList.size() == 0) {
            return null;
        }
        Qr qr = qrList.get(0);

        if (qr == null) {
            return null;
        }

        // 2. 从二维码信息中获取批次ID并查询批次信息
        Long patchId = qr.getQrPatchID();
        Patch patch = patchMapper.selectPatchByPatchID(patchId);
        return patch;

    }
}
